#include <REGX52.H>
#include <INTRINS.h>
#include "LCD1602.h"
#include "Delay.h"
#include "Key.h"
#include "AT24C02.h"
#include "I2C.h"
unsigned char Data;
unsigned char KeyNum;
unsigned int Num;
void main()
	
{
	  LCD_Init();
//    AT24C02_WriteByte(1,22);//字地址 0~255
//    Delay(5);//写周期5ms 写完是立马读不出来的 需要时间写入
//	  Data=AT24C02_ReadByte(1);//读取
//	  LCD_ShowNum(1,1,Data,3);
		while(1)
		{
		 KeyNum=Key();
		if(KeyNum==1)	//K1按键，Num自增
		{
			Num++;
			LCD_ShowNum(1,1,Num,5);
		}
		if(KeyNum==2)	//K2按键，Num自减
		{
			Num--;
			LCD_ShowNum(1,1,Num,5);
		}
		if(KeyNum==3)	//K3按键，向AT24C02写入数据
		{
			AT24C02_WriteByte(0,Num%256);
			Delay(5);
			AT24C02_WriteByte(1,Num/256);
			Delay(5);
			LCD_ShowString(2,1,"Write OK");
			Delay(1000);
			LCD_ShowString(2,1,"        ");
		}
		if(KeyNum==4)	//K4按键，从AT24C02读取数据
		{
			Num=AT24C02_ReadByte(0);
			Num|=AT24C02_ReadByte(1)<<8;
			LCD_ShowNum(1,1,Num,5);
			LCD_ShowString(2,1,"Read OK ");
			Delay(1000);
			LCD_ShowString(2,1,"        ");
		}
		}

}